package com.lib76.jsp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 全局变量设置拦截器
 * @author 张浩
 * @version 0.0.1
 */
public class GlobalParameterInterceptor implements HandlerInterceptor{

	public void afterCompletion(HttpServletRequest req,
			HttpServletResponse resp, Object obj, Exception ex)
			throws Exception {
	}

	public void postHandle(HttpServletRequest req, HttpServletResponse resp,
			Object obj, ModelAndView model) throws Exception {
		if(model != null){
			//得到协议如：http
			String scheme = req.getScheme();
			//得到服务器名称如：127.0.0.1
			String serverName = req.getServerName();
			//得到端口号如8080
			int serverPort = req.getServerPort();
			//得到当前上下文路径，也就是安装后的文件夹位置。
			String contextPath = req.getContextPath();
			//连起来拼成完整的url
			String webRoot = scheme+"://"+serverName+":"+serverPort + contextPath;
			model.getModelMap().addAttribute("webRoot", webRoot);
		}
	}

	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp,
			Object obj) throws Exception {
		HttpServletUtils.Request.set(req);
		HttpServletUtils.Response.set(resp);
		return true;
	}

}
